"""
@author: shaoshuai
@file :student.py
@time: 2021/12/29 21:07
"""

from interface import student_interface
from interface import common_interface
from lib import common


# 记录当前用户登录状态
student_info = {
    'user' : None
}


# 注册功能
def register():
    while True:
        username = input('请输入您的用户名：').strip()
        password = input('请输入密码：').strip()
        re_password = input('请确认密码：').strip()

        # 小的逻辑判断
        if password == re_password:
            # 调用学生注册接口
            flag, msg = student_interface.student_register_interface(
                username, password
            )

            if flag:
                print(msg)
                break
            else:
                print(msg)
        else:
            print('两次密码不一致，请重新输入！')


# 登录功能
def login():
    while True:
        username = input('请输入您的用户名：').strip()
        password = input('请输入密码：').strip()
        # 调用登录接口
        flag, msg = common_interface.login_interface(
            username, password, user_type='student'
        )
        if flag:
            print(msg)
            # 记录当前登录状态
            student_info['user'] = username
            break
        else:
            print(msg)


# 选择学校功能
@common.auth('student')
def choice_school():
    while True:
        # 得到所有的学校
        flag, school_list_msg = common_interface.get_all_school_interface()
        if not flag:
            print(school_list_msg)
            break

        for index, school_name in enumerate(school_list_msg):
            print(f'编号：[{index}]  学校名称：[{school_name}]')

        choice = input('请输入您选择的学校：').strip()

        if not choice.isdigit():
            print('请输入数字！')
            continue

        choice = int(choice)

        if choice not in range(len(school_list_msg)):
            print('请输入正确的编号！')

        school_name = school_list_msg[choice]

        # 调用学校接口下的select方法，实例化得到对象
        flag, msg = student_interface.choice_school_interface(
            school_name, student_info['user']
        )

        if flag:
            print(msg)
            break
        else:
            print(msg)
            break

# 选择课程功能
@common.auth('student')
def choice_course():
    pass

# 查看分数功能
@common.auth('student')
def select_score():
    pass

# 函数字典
func_dic = {
    '1': register,
    '2': login,
    '3': choice_school,
    '4': choice_course,
    '5': select_score,
}



# 功能函数
def student_view():
    while True:
        print('''
        =========欢迎来到学生视图层==========
                    - 1.注册
                    - 2.登录功能
                    - 3.选择校区
                    - 4.选择课程
                    - 5.查看分数
        ============24小时为您服务===========
        ''')

        choice = input('请选择功能编号（输入q退出）: ').strip()

        if choice == 'q':
            break

        if not choice.isdigit():
            print('请输入数字！')
            continue

        if choice not in func_dic:
            print('请输入正确的编号！')
            continue

        func_dic.get(choice)()